package com.arcsoft.arcfacedemo.util;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;

public class ImageUtils {
    public ImageUtils() {
    }

    public static byte[] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {
        int[] argb = new int[inputWidth * inputHeight];
        scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
        byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];
        encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
        return yuv;
    }

    private static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
        int frameSize = width * height;
        int yIndex = 0;
        int uvIndex = frameSize;
        int index = 0;

        for(int j = 0; j < height; ++j) {
            for(int i = 0; i < width; ++i) {
                int R = (argb[index] & 16711680) >> 16;
                int G = (argb[index] & '\uff00') >> 8;
                int B = argb[index] & 255;
                int Y = (66 * R + 129 * G + 25 * B + 128 >> 8) + 16;
                int U = (-38 * R - 74 * G + 112 * B + 128 >> 8) + 128;
                int V = (112 * R - 94 * G - 18 * B + 128 >> 8) + 128;
                yuv420sp[yIndex++] = (byte)(Y < 0 ? 0 : (Y > 255 ? 255 : Y));
                if (j % 2 == 0 && index % 2 == 0 && uvIndex < yuv420sp.length - 2) {
                    yuv420sp[uvIndex++] = (byte)(V < 0 ? 0 : (V > 255 ? 255 : V));
                    yuv420sp[uvIndex++] = (byte)(U < 0 ? 0 : (U > 255 ? 255 : U));
                }

                ++index;
            }
        }

    }

    public static void nv21ToNv12(byte[] nv21, byte[] nv12, int width, int height) {
        if (nv21 != null && nv12 != null) {
            int framesize = width * height;
            System.arraycopy(nv21, 0, nv12, 0, framesize);

            for(int i = 0; i < framesize; ++i) {
                nv12[i] = nv21[i];
            }

            int j;
            for(j = 0; j < framesize / 2; j += 2) {
                nv12[framesize + j - 1] = nv21[j + framesize];
            }

            for(j = 0; j < framesize / 2; j += 2) {
                nv12[framesize + j] = nv21[j + framesize - 1];
            }

        }
    }

    public static void mirrorNV21(byte[] nv21Data, int width, int height) {
        int i;
        int left;
        int right;
        byte temp;
        for(i = 0; i < height; ++i) {
            left = width * i;

            for(right = width * (i + 1) - 1; left < right; --right) {
                temp = nv21Data[left];
                nv21Data[left] = nv21Data[right];
                nv21Data[right] = temp;
                ++left;
            }
        }

        int offset = width * height;

        for(i = 0; i < height / 2; ++i) {
            left = offset + width * i;

            for(right = offset + width * (i + 1) - 2; left < right; --right) {
                temp = nv21Data[left];
                nv21Data[left] = nv21Data[right];
                nv21Data[right] = temp;
                ++left;
                --right;
                temp = nv21Data[left];
                nv21Data[left] = nv21Data[right];
                nv21Data[right] = temp;
                ++left;
            }
        }

    }

    public static void mirror(byte[] src, int w, int h) {
        int i;
        byte temp;
        int a;
        int b;
        for(i = 0; i < h; ++i) {
            a = i * w;

            for(b = (i + 1) * w - 1; a < b; --b) {
                temp = src[a];
                src[a] = src[b];
                src[b] = temp;
                ++a;
            }
        }

        int index = w * h;

        for(i = 0; i < h / 2; ++i) {
            a = i * w;

            for(b = (i + 1) * w - 2; a < b; b -= 2) {
                temp = src[a + index];
                src[a + index] = src[b + index];
                src[b + index] = temp;
                temp = src[a + index + 1];
                src[a + index + 1] = src[b + index + 1];
                src[b + index + 1] = temp;
                a += 2;
            }
        }

    }

    private Rect adjustRect(Rect rect, int height, int width, int previewHeight, int previewWidth) {
        Rect rectRes = new Rect(rect);
        rect.right = previewHeight - rectRes.top;
        rect.left = previewHeight - rectRes.bottom;
        rect.top = rectRes.right;
        rect.bottom = rectRes.left;
        float ratioHorizontal = (float)previewHeight / (float)width;
        float ratioVertical = (float)previewWidth / (float)height;
        rectRes.left = (int)((float)rect.left / ratioHorizontal);
        rectRes.right = (int)((float)rect.right / ratioHorizontal);
        rectRes.top = (int)((float)rect.top / ratioVertical);
        rectRes.bottom = (int)((float)rect.bottom / ratioVertical);
        return rectRes;
    }

    public static Bitmap yuvToBitmap(byte[] data, int width, int height) {
        int frameSize = width * height;
        int[] rgba = new int[frameSize];

        for(int i = 0; i < height; ++i) {
            for(int j = 0; j < width; ++j) {
                int y = 255 & data[i * width + j];
                int u = 255 & data[frameSize + (i >> 1) * width + (j & -2)];
                int v = 255 & data[frameSize + (i >> 1) * width + (j & -2) + 1];
                y = y < 16 ? 16 : y;
                int r = Math.round(1.164F * (float)(y - 16) + 1.596F * (float)(v - 128));
                int g = Math.round(1.164F * (float)(y - 16) - 0.813F * (float)(v - 128) - 0.391F * (float)(u - 128));
                int b = Math.round(1.164F * (float)(y - 16) + 2.018F * (float)(u - 128));
                r = r < 0 ? 0 : (r > 255 ? 255 : r);
                g = g < 0 ? 0 : (g > 255 ? 255 : g);
                b = b < 0 ? 0 : (b > 255 ? 255 : b);
                rgba[i * width + j] = -16777216 + (b << 16) + (g << 8) + r;
            }
        }

        Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        bmp.setPixels(rgba, 0, width, 0, 0, width, height);
        return bmp;
    }

    public static Bitmap rotateBitmap(Bitmap origin, float alpha) {
        if (origin == null) {
            return null;
        } else {
            int width = origin.getWidth();
            int height = origin.getHeight();
            Matrix matrix = new Matrix();
            matrix.setRotate(alpha);
            Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
            if (newBM.equals(origin)) {
                return newBM;
            } else {
                origin.recycle();
                return newBM;
            }
        }
    }

    public static boolean isFaceLocationOK(Rect faceRect, int previewWidth, int previewHeight) {
        return (double)faceRect.left > 0.2D * (double)previewWidth && (double)faceRect.right < 0.7D * (double)previewWidth && (double)faceRect.bottom < 0.75D * (double)previewHeight && (double)faceRect.top > 0.25D * (double)previewHeight;
    }

    public static Bitmap imageCrop(Bitmap bitmap, Rect rect) {
        return Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), (Matrix)null, false);
    }
}
